package cn.tedu;


import org.junit.Test;

import java.sql.*;

public class Demo03 {

    @Test
    public void insert() throws SQLException {
        System.out.println("insert");
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/newdb3?characterEncoding=utf8&serverTimezone=Asia/Shanghai",
                "root","root");
        Statement s = conn.createStatement();
        s.executeUpdate("insert into emp(name) values('刘德华')");
        conn.close();
    }

    @Test
    public void update() throws SQLException {
        System.out.println("update");
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/newdb3?characterEncoding=utf8&serverTimezone=Asia/Shanghai",
                "root", "root");
        Statement s = conn.createStatement();
        s.executeUpdate("update emp set name='张学友' where name='刘德华'");
        conn.close();
    }

    @Test
    public void delete() throws SQLException {
        System.out.println("delete");
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/newdb3?characterEncoding=utf8&serverTimezone=Asia/Shanghai",
                "root", "root");
        Statement s = conn.createStatement();
        s.executeUpdate("delete from emp where name='张学友'");
        conn.close();
    }

    @Test
    public void select() throws SQLException {
        System.out.println("select");
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/newdb3?characterEncoding=utf8&serverTimezone=Asia/Shanghai",
                "root", "root");
        Statement s = conn.createStatement();
        ResultSet rs = s.executeQuery("select name,sal from emp");
        //遍历结果集对象,rs.next()返回值布尔值表示是否有下一条数据,同时游标向下移动
        while (rs.next()){
            String name = rs.getString("name");
            double sal = rs.getDouble("sal");
            System.out.println("name:"+name+",sal:"+sal);
        }
        conn.close();
    }
}
